/**
 * Created by Tazyong on 2017/5/17.
 */

export default (function () {
  let isInit = false,
    basisFontSize, basisHeight;

  /*********************** 适配屏幕 ***********************/
  function setSize(appElement) {
    let
      html = document.querySelector('html'),
      body = document.body,
      viewportWidth = window.innerWidth,
      viewportHeight = window.innerHeight;

    //获取基础字体和基础高度
    if (!isInit) {
      basisFontSize = parseFloat(getComputedStyle(html).fontSize);
      basisHeight = parseFloat(getComputedStyle(html).minHeight);
      isInit = true;
    }

    //设置根字体大小
    html.style.fontSize
      = viewportHeight / basisHeight * basisFontSize + 'px';

    //固定 body 高度
    body.style.height = viewportHeight + 'px';

    //适配宽高比
    viewportWidth / viewportHeight > 9 / 11
      ? appElement.style.width = viewportHeight / 14 * 9 + 'px'
      : appElement.removeAttribute('style');

    //显示元素
    appElement.style.display = 'block';
  }

  /*********************** 设置 transform ***********************/
  function setTransform(element, value) {
    if (element && element.nodeType === 1) {
      element.style.transform = value;
      element.style.webkitTransform = value;
      element.style.mozTransform = value;
      element.style.msTransform = value;
      element.style.oTransform = value;
      return true;
    } else {
      return false;
    }
  }

  /*********************** 获取 transform ***********************/
  function getTransform(element) {
    let transform = getComputedStyle(element).transform,
      webkitTransform = getComputedStyle(element).webkitTransform,
      mozTransform = getComputedStyle(element).mozTransform,
      msTransform = getComputedStyle(element).msTransform,
      oTransform = getComputedStyle(element).oTransform;
    return transform
      || webkitTransform
      || mozTransform
      || msTransform
      || oTransform;
  }

  /*********************** 设置 transformOrigin ***********************/
  function setTransformOrigin(element, value) {
    if (element && element.nodeType === 1) {
      element.style.transformOrigin = value;
      element.style.webkitTransformOrigin = value;
      element.style.mozTransformOrigin = value;
      element.style.msTransformOrigin = value;
      element.style.oTransformOrigin = value;
      return true;
    } else {
      return false;
    }
  }

  /*********************** 设置 transition ***********************/
  function setTransition(element, value) {
    if (element && element.nodeType === 1) {
      element.style.transition = value;
      element.style.webkitTransition = value;
      element.style.mozTransition = value;
      element.style.oTransition = value;
      return true;
    } else {
      return false;
    }
  }

  /****************** 返回对象构造方法 ******************/
  return {
    setSize,
    setTransform,
    getTransform,
    setTransformOrigin,
    setTransition
  }
})();
